﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SolrExample.Interface.DomainObject;
using System.Data.Common;
using System.Data;

namespace SolrExample.Data
{
    public partial class JobAdDao
    {
        private string dbCountryCode;
        private string dbConnectionString;
        private DbProviderFactory dbProviderFactory;
        private int commandExecutionTimeOut = 300;

        public JobAdDao(string connectionString, string connectionProviderName, string countryCode) { 
            if (string.IsNullOrEmpty(connectionString))
            {
                throw new ArgumentNullException("connectionString");
            }
            if (string.IsNullOrEmpty(connectionProviderName))
            {
                connectionProviderName = "System.Data.SqlClient";
            }
            if (string.IsNullOrEmpty(countryCode))
            {
                throw new ArgumentNullException("countryCode");
            }

            dbConnectionString = connectionString;
            dbProviderFactory = DbProviderFactories.GetFactory(connectionProviderName);
            dbCountryCode = countryCode;
        }

        public TimeSpan CommandExecutionTimeOut {
            get {
                return TimeSpan.FromSeconds(this.commandExecutionTimeOut);
            }
            set {
                var commandExecutionTimeOut = (int)Math.Ceiling(value.TotalSeconds);
                if (commandExecutionTimeOut < 0) { 
                    throw new Exception("commandExecutionTimeOut must be >= 0");
                }
                this.commandExecutionTimeOut = commandExecutionTimeOut;
            }
        }

        public IList<long> GetJobIndex() {
            string sql = "Select Id From JobAd JA With(Nolock) Where JA.utcLastModifiedTime>JA.utcIndexLastUpdatedTime";
            var jobIds = new List<long>();
            using (var connection = dbProviderFactory.CreateConnection()) 
            {
                connection.ConnectionString = dbConnectionString;
                connection.Open();
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = sql;
                    command.CommandType = CommandType.Text;
                    command.CommandTimeout = commandExecutionTimeOut;
                    
                    using (var reader = command.ExecuteReader()) {
                        var ordId = reader.GetOrdinal("Id");
                        while (reader.Read()) {
                            var jobId = reader.GetInt64(ordId);
                            jobIds.Add(jobId);

                        }
                    }
                }
            }
            return jobIds;
        }

        public IJobAd GetJobAdDetails(long jobAdId)
        {
            IJobAd jobAd = null;

            jobAd = new JobAd("HK", 1, 11, "adsRef", "company", "companyAltLang", "companyDesc", DateTime.Now.AddDays(1), null, 'A', null, "posSeek");

            return jobAd;
        }
    }
}
